(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:StepfreightCtrl
     * @description
     * # StepfreightCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('StepfreightCtrl', StepfreightCtrl);

    StepfreightCtrl.$inject = ['$scope', 'dataService', '$location', 'helper', '$rootScope', '$timeout', 'sessionService'];

    function StepfreightCtrl($scope, dataService, $location, helper, $rootScope, $timeout, sessionService) {

        $scope.listAction = function() {
            $scope.pageSize = 10;
            $scope.getList(1);
            $scope.$on('event:up-store-id', function() {
                $scope.store_id = $rootScope.store_id
                $scope.getList(1);
            });
        }

        $scope.getList = function(pageNo) {
            var query = { page_no: pageNo ? pageNo : "1", page_size: $scope.pageSize, order_by: "freight_rule_id:desc" };
            query.query = { "store_id": $rootScope.store_id ? $rootScope.store_id : $scope.store_id };

            dataService.stepfreightGet(query).then(function(list) {
                if (list.freight_rule) {
                    $scope.freight_rule = list.freight_rule;
                    $scope.total_count = list.total_count;
                }
            });
        }

        $scope.freightruledata = function(ruleInfo) {
            console.error("ruleInfo--->", ruleInfo);
        }

        $scope.addAction = function() {
            var freight_rule_id = $scope.freight_rule_id = $location.search().freight_rule_id;

            $scope.stepData = {};
            if (freight_rule_id) {
                $scope.getOne(freight_rule_id);
            } else {
                $scope.stepData = {
                    "store_id": $rootScope.store_id ? $rootScope.store_id : $scope.store_id,
                    "base_fee": 0, //基础运费
                    "distance_fees": [], //距离额外配送费
                    "weight_fees": [], //重量额外配送费
                    "period_fees": [], //时段额外配送费
                };
            }
            console.log("stepData---->", $scope.stepData);
        }

        //
        $scope.getOne = function(freight_rule_id) {
            var query = { "query": { "freight_rule_id": freight_rule_id } };

            dataService.stepfreightGet(query).then(function(result) {
                if (result.total_count) {
                    $scope.stepData = result.freight_rule[0];
                    if (!$scope.stepData.distance_fees) {
                        $scope.stepData.distance_fees = [];
                    }
                    if (!$scope.stepData.weight_fees) {
                        $scope.stepData.weight_fees = [];
                    }
                    if (!$scope.stepData.period_fees) {
                        $scope.stepData.period_fees = [];
                    }
                    $scope.freight_rule_id = $scope.stepData.freight_rule_id;
                }
            });
        }

        //删除距离额外配送
        $scope.deleteDistance = function(key) {
            $scope.stepData.distance_fees.splice(key, 1);
        }

        //距离额外配送
        $scope.addDistance = function() {
            $scope.stepData.distance_fees.push({ "lower": 0, "upper": 3, "unit": 0, "unit_fee": 1 });
        }

        //删除重量额外配送
        $scope.deleteWeight = function(key) {
            $scope.stepData.weight_fees.splice(key, 1);
        }

        //重量额外配送
        $scope.addWeight = function() {
            $scope.stepData.weight_fees.push({ "lower": 6, "upper": 10, "unit": 0, "unit_fee": 1 });
        }

        //删除时段额外配送
        $scope.deletePeriod = function() {
            $scope.stepData.period_fees.splice(key, 1);
        }

        //时段额外配送
        $scope.addPeriod = function() {
            $scope.stepData.period_fees.push({ "start": "00:00", "end": "7:00", "unit_fee": 1 });
        }

        $scope.stepfreightAdd = function() {
            var data = $scope.refreshData();

            if (!data)
                return false;

            if ($scope.stepData.freight_rule_id) {
                console.error("data----------->", data);
                dataService.stepfreightUpdate(data).then(function(res) {
                    if (res.freight_rule_id) {
                        helper.myAlert("success", "#/stepfreight/list");
                    } else {
                        helper.myAlert("error：" + res.Msg);
                        return false;
                    }
                });
            } else {
                if (!data.distance_fees && !data.weight_fees && !data.period_fees) {
                    helper.myAlert("请设置阶梯运费！");
                    return false;
                }

                if (!data)
                    return false;

                dataService.stepfreightAdd(data).then(function(res) {
                    if (res.freight_rule_id) {
                        helper.myAlert("success", "#/stepfreight/list");
                    } else {
                        helper.myAlert("error：" + res.Msg);
                        return false;
                    }
                });
            }
        }

        //重新整理一下添加的数据
        $scope.refreshData = function() {
            var data = {};

            if ($scope.stepData.freight_rule_id) {
                data.freight_rule_id = $scope.stepData.freight_rule_id;
            }

            data.store_id = $rootScope.store_id;
            data.base_fee = $scope.stepData.base_fee;

            if ($scope.stepData.distance_fees.length > 0) {
                data.distance_fees = [];
                for (var i in $scope.stepData.distance_fees) {
                    var a = {
                        "no": i * 1 + 1,
                        "lower": $scope.stepData.distance_fees[i].lower,
                        "upper": $scope.stepData.distance_fees[i].upper,
                        "unit": $scope.stepData.distance_fees[i].unit,
                        "unit_fee": parseFloat($scope.stepData.distance_fees[i].unit_fee),
                    }
                    if ($scope.stepData.distance_fees[i].distance_fee_id) {
                        a.distance_fee_id = $scope.stepData.distance_fees[i].distance_fee_id;
                    }
                    data.distance_fees.push(a);
                }
            }

            if ($scope.stepData.weight_fees.length > 0) {
                data.weight_fees = [];
                for (var i in $scope.stepData.weight_fees) {
                    var a = {
                        "no": i * 1 + 1,
                        "lower": $scope.stepData.weight_fees[i].lower,
                        "upper": $scope.stepData.weight_fees[i].upper,
                        "unit": $scope.stepData.weight_fees[i].unit,
                        "unit_fee": parseFloat($scope.stepData.weight_fees[i].unit_fee),
                    }
                    if ($scope.stepData.weight_fees[i].weight_fee_id) {
                        a.weight_fee_id = $scope.stepData.weight_fees[i].weight_fee_id;
                    }
                    data.weight_fees.push(a);
                }
            }

            if ($scope.stepData.period_fees.length > 0) {
                data.period_fees = [];
                for (var i in $scope.stepData.period_fees) {
                    var a = {
                        "no": i * 1 + 1,
                        "start": $scope.stepData.period_fees[i].start,
                        "end": $scope.stepData.period_fees[i].end,
                        "unit_fee": parseFloat($scope.stepData.period_fees[i].unit_fee),
                    }
                    if ($scope.stepData.period_fees[i].period_fee_id) {
                        a.period_fee_id = $scope.stepData.period_fees[i].period_fee_id;
                    }
                    data.period_fees.push(a);
                }
            }

            return data;
        }

        //删除
        $scope.carryModesDelete = function(fare_id, id, flag, index) {
            var data = {};

            if (fare_id) {
                data.fare_id = fare_id;
            }

            if (id) {
                data.id = id;
            } else {
                $timeout(function() {
                    $scope.fareTemplate.carry_modes[flag].son_data.splice(index, 1);
                });
                return false;
            }
            dataService.postfeeDelete(data).then(function(r) {
                if (r.Msg) {
                    helper.myAlert("erro:" + r.Msg);
                } else {
                    helper.myAlert("success", null, null, function() {
                        $scope.getOne(data.fare_id);
                    });
                }
            });
        }

        //运费删除
        $scope.freightRuleDelete = function(freight_rule_id) {
            helper.myConfirm('确定删除？', function() {
                var data = {};
                if (freight_rule_id) {
                    data.freight_rule_id = freight_rule_id;
                }

                dataService.stepfreightDelete(data).then(function(r) {
                    if (r.Msg) {
                        helper.myAlert("erro:" + r.Msg);
                    } else {
                        helper.myAlert("success", null, null, function() {
                            $scope.listAction();
                        });
                    }
                });
            });
        }
    }
})();